package cn.com.bsoft.cloud;

import cn.com.bsoft.cloud.entity.company.PTCompanyMess;
import cn.com.bsoft.cloud.service.CloudCompanyService;
import cn.com.bsoft.cloud.utils.AesUtils;
import org.apache.tomcat.util.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
// 定义filterName 和过滤的url
@WebFilter(filterName = "companyFilter" ,urlPatterns = "/cloudcompany/*")
public class CompanyFilter implements Filter {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private CloudCompanyService cloudCompanyService;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        ServletContext sc = filterConfig.getServletContext();
        WebApplicationContext cxt = WebApplicationContextUtils.getWebApplicationContext(sc);
        if(cxt != null && cxt.getBean(CloudCompanyService.class) != null && cloudCompanyService == null) {
            cloudCompanyService =cxt.getBean(CloudCompanyService.class);
        }
        logger.info("TestFilter init");
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        ServletRequest requestWrapper = null;
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String token = request.getHeader("token");
        String companyCode = request.getHeader("companyCode");
        WrapperResponse wrapResponse = new WrapperResponse((HttpServletResponse)servletResponse);


        PTCompanyMess companyMess = cloudCompanyService.getCompanyMess(companyCode);

        if(servletRequest instanceof HttpServletRequest || token.isEmpty() || companyCode.isEmpty()) {
            requestWrapper = new CompanyHttpServletRequestWrapper((HttpServletRequest) servletRequest,companyMess,token);
            filterChain.doFilter(requestWrapper, wrapResponse);

            String data = wrapResponse.getContent();
            byte[] aesData = AesUtils.encrypt(data,companyMess.getSecret());
            String  aesBase64Str = Base64.encodeBase64String(aesData);

            HttpServletResponse httpServletResponse = (HttpServletResponse) servletResponse;
            httpServletResponse.getOutputStream().write(aesBase64Str.getBytes("utf-8"));
            httpServletResponse.setContentLength(aesBase64Str.getBytes("utf-8").length);
            httpServletResponse.flushBuffer();

            byte[] bb = Base64.decodeBase64(aesBase64Str);
            byte[] desByte = AesUtils.decrypt(bb,companyMess.getSecret());
            String dec = new String(desByte,"utf-8");
        }
        else{
            filterChain.doFilter(servletRequest, servletResponse);
        }

    }

    @Override
    public void destroy() {

    }

}
